package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotAlarmTriggerCondition;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotAlarmTriggerConditionDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2021-03-04 19:17:12
 */
public class IotAlarmTriggerConditionConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotAlarmTriggerConditionConverts.class);

    public static List<IotAlarmTriggerConditionDTO> convert(List<IotAlarmTriggerCondition> iotAlarmTriggerConditionList) {
        if (CollectionUtils.isEmpty(iotAlarmTriggerConditionList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotAlarmTriggerConditionDTO> iotAlarmTriggerConditionListResult = Lists.newArrayList();
        for (IotAlarmTriggerCondition iotAlarmTriggerCondition : iotAlarmTriggerConditionList) {
            iotAlarmTriggerConditionListResult.add(convert(iotAlarmTriggerCondition));
        }
        return iotAlarmTriggerConditionListResult;
    }

    public static IotAlarmTriggerConditionDTO convert(IotAlarmTriggerCondition iotAlarmTriggerCondition) {
        try {
            return BeanUtils.copy(iotAlarmTriggerCondition, IotAlarmTriggerConditionDTO.class);
        } catch (Exception e) {
            LOG.error("IotAlarmTriggerCondition到IotAlarmTriggerConditionDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotAlarmTriggerCondition到IotAlarmTriggerConditionDTO转换失败", e);
        }
    }

    public static IotAlarmTriggerCondition convert(IotAlarmTriggerConditionDTO iotAlarmTriggerConditionDTO) {
        try {
            return BeanUtils.copy(iotAlarmTriggerConditionDTO, IotAlarmTriggerCondition.class);
        } catch (Exception e) {
            LOG.error("IotAlarmTriggerConditionDTO到IotAlarmTriggerCondition转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotAlarmTriggerConditionDTO到IotAlarmTriggerCondition转换失败", e);
        }
    }
}